const app = getApp()
var upload = require('../../utils/upload');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width: 130,
    store: [],
    storeImg:[],
    tempFilePaths: [],
    tempFileNum:0,
    checked: true
  },
  //打开载入
  onLoad: function () {
    this.getWidghHeight();
    this.getApi();
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    app.isLogin();
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    this.setData({
      skeleton: false,
    })
  },
  /**
  * 修改安全状态
  */
  onChange(event) {
    this.setData({ checked: event.detail });
  },
  /**
   * 读取我的订单
   */
  getApi: function () {
    let that = this;
    let checked = false;
    app.api().Get("api/v1/allwin-store-manage", function (result) {
      checked = result.data.state == 1 ? true : false;
      var imgsLen = result.data.imgs.length, imgsLen = imgsLen >= 9 ? 9 : imgsLen;
      that.setData({
        store: result.data,
        storeImg: result.data.imgs,
        tempFileNum: imgsLen,
        checked: checked
      });
    })
  },
  /**
   * 提交绑定
   */
  formSubmit: function (e) {
    let that = this;
    var data = e.detail.value;
    var isPost = false;
    var msg = '';
    if (app.util().isNull(data.name)) {
      msg = '好店名称必须填写'
    } else if (app.util().isNull(data.work_time)) {
      msg = '营业时间必须填写'
    } else if (app.util().isNull(data.address)) {
      msg = '好店地址必须填写'
    } else if (app.util().isNull(data.telphone)) {
      msg = '联系电话必须填写'
    } else if (app.util().isNull(data.tips)) {
      msg = '广告短语必须填写'
    } else if (app.util().isNull(data.money_coupon)) {
      msg = '满多少可领取赠品'  
    } else {
      isPost = true;
      wx.showLoading({ title: '提交中', mask: true })
      //商城图片并提交
      that.postFile(function (res) {
        if (res.isUpload) {
          var parms = {
            state: that.data.checked,
            name: data.name,
            work_time: data.work_time,
            address: data.address,
            telphone: data.telphone,
            tips: data.tips,
            money_coupon: data.money_coupon,
            imgs: that.data.storeImg,
            upload: res.filePaths
          }
          app.api().Post('api/v1/allwin-store-editstore', parms, function (res) {
            wx.hideLoading();
            app.wxAlert(res.msg);
          })
        }
      })
     
    }
    if (!isPost) {
      wx.showModal({
        content: msg
      })
    }
  },
  /**
 * 获取图片宽高
 */
  getWidghHeight: function () {
    let that = this;
    const query = wx.createSelectorQuery()
    query.select('.select_img').boundingClientRect(function (res) {
      that.setData({
        width: parseInt(res.width)
      })
    }).exec();
  },
  /**
    * 图片上传
    */
  postFile: function (callback) {
    let that = this;
    var tempImg = that.data.tempFilePaths;
    var imgs = new Array();
    var count = app.util().count(tempImg);
    if (count == 0) {
      callback && callback({
        isUpload: true,
        filePaths: imgs
      });
    } else {
      var isUpload = false;
      for (var i = 0; i < count; i++) {
        upload.Upload('api/v1/allwin-store-upimg', {
          tempPath: tempImg[i]
        }, function (res) {
          if (res.code == 200) {
            imgs.push(res.data);
          } else {
            count = count - 1;
          }
          if (count == imgs.length) {
            callback && callback({
              isUpload: true,
              filePaths: imgs
            });
          }
        }, function (res) {
          console.log(res.progress);
        });
      }
    }
  },
  /**
   * 选择图片
   */
  selectImg: function () {
    let that = this;
    var tempFileNum = that.data.tempFileNum,img = that.data.tempFilePaths;
    if (tempFileNum >= 9) {
      wx.showModal({
        showCancel: false,
        content: '最多上传9张图片'
      })
    } else {
        wx.chooseImage({ 
        count:1,sizeType: ['compressed'],
        success: function (res) {
          var src = res.tempFilePaths;
          var serlen = src.length;
          for (var i = 0; i < serlen; i++) {
            img.push(src[i]);
          }
          that.setData({
            tempFileNum: tempFileNum+1,
            tempFilePaths: img,
            isUpload: false
          })
        }
      })
    }
  },
  /**
   * 删除上传图片
   */
  delUpImg: function (res) {
    let that = this;
    var array = this.data.tempFilePaths,index = res.target.id, tempFileNum = parseInt(that.data.tempFileNum);
    if (index > -1) {
      array.splice(index, 1);
      tempFileNum = tempFileNum - 1;
      if (tempFileNum < 0) {
        tempFileNum = 0;
      }
    }
    that.setData({
      tempFilePaths: array,
      tempFileNum: tempFileNum
    })
  },
  /**
   * 删除已商城图片
   */
  delImg: function (res) {
    let that = this;
    var array = this.data.storeImg, index = res.target.id, tempFileNum = parseInt(that.data.tempFileNum);
    if (index > -1) {
      array.splice(index, 1);
      tempFileNum = tempFileNum-1;
      if (tempFileNum < 0){
        tempFileNum = 0;
      }
    }
    that.setData({
      storeImg: array,
      tempFileNum: tempFileNum
    })
  }
})